<template>
  <a-modal
    :title="title"
    :width="460"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
    okText="保存">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="绣字图片"
          hasFeedback >
          <a-input placeholder="请输入绣字图片名称" v-model="category_name"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="编号"
          hasFeedback >
          <a-input placeholder="请输入编号" v-model="category_code" v-show="this.title=='编辑绣字图片'" disabled="disabled"/>
          <a-input placeholder="请输入编号" v-model="category_code" v-show="this.title=='添加绣字图片'"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="排序"
          hasFeedback >
          <a-input placeholder="请输入排序" v-model="single_price"/>
        </a-form-item>

<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="绣字选择"-->
<!--          hasFeedback >-->
<!--          <a-select v-model="select_type" placeholder="请选择绣字类型" style="color: #a1a19f;">-->
<!--            <a-select-option :value="item.id" v-for="item in couponList" >{{ item.name }}</a-select-option>-->
<!--          </a-select>-->
<!--        </a-form-item>-->


<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="绣字大小"-->
<!--          hasFeedback >-->
<!--          <a-input placeholder="请输入绣字大小" v-model="xiuzi_size"/>-->
<!--        </a-form-item>-->

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="图片上传"
          hasFeedback >
          <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-image-upload class="avatar-uploader" text="上传" v-model="fileList" style="margin-top: 8px;"></j-image-upload>
          </a-form-item>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注"
          style="margin-top: -60px;" >
          <a-textarea placeholder="请输入备注信息" v-model="remark" rows="3"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction, postAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JImageUpload from '../../../components/jeecg/JImageUpload'
  import Qs from "qs";

  export default {
    name: "JeecgDemoModal",
    components: {
      JImageUpload
    },
    data () {
      return {
        category_name:'',
        category_code:'',
        single_price:'0',
        remark:'',
        select_type:'请选择绣字类型',//类型
        xiuzi_size:'',//大小

        fileList: [],
        categorydetail:[],
        couponList: [
          {
            id: '0',
            name: '绣字字体'
          },
          {
            id: '1',
            name: '绣字图片'
          }
        ],

        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        },
        url: {
          fileUpload: window._CONFIG['domianURL'] + "/sys/common/upload",
          query_detail:"/admin/parameter/test_xiuzi_fontImgDetail",
          add: "/admin/parameter/add_xiuzi_fontImg",
          edit: "/admin/parameter/update_xiuzi_fontImg"
        },
      }
    },
    computed: {
      uploadAction: function() {
        return this.url.fileUpload;
      }
    },
    created () {
    },
    methods: {
      edit (record) {
        this.category_name='';
        this.category_code='';
        this.select_type='请选择绣字类型';
        this.single_price='0';
        this.remark='';
        this.xiuzi_size='';
        this.fileList='';
        this.model = Object.assign({}, record);
        this.visible = true;//开启弹框
        if(record=="0"){
          this.title="添加绣字图片"
        }else {
          this.title="编辑绣字图片"
          this.show_categorydetail(this.model.id);
        }
      },
      show_categorydetail(id) {
        let params = {id:id};
        getAction(this.url.query_detail,params).then((res)=>{
          if(res.success){
            this.categorydetail = res.result;
            this.category_name=this.categorydetail.embroideredName;
            this.category_code=this.categorydetail.code;
            this.single_price=this.categorydetail.numbers;
            this.remark=this.categorydetail.remark;
            this.xiuzi_size=this.categorydetail.xiuziSize;
            if(this.categorydetail.isZitiImg=='0'){
              this.select_type='绣字字体';
            }else if(this.categorydetail.isZitiImg=='1'){
              this.select_type='绣字图片';
            }
            setTimeout(() => {
              this.fileList = this.categorydetail.embroideredImage;
            }, 5)

          }else {
            this.$message.error("查询失败");
          }
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if(this.category_name==null || this.category_name==''){
            this.$message.warning("请输入绣字名称");
            return;
          }
          else if(this.category_code=='' || this.category_code==null){
            this.$message.warning("请输入编号");
            return;
          }
          else if(this.single_price=='' || this.single_price==null){
            this.$message.warning("请输入排序");
            return;
          }else if(this.fileList == null || this.fileList.length <= 0){
            this.$message.warning("请上传图片");
            return;
          }
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            let params = '';
            if(this.model.id!=null && this.model.id!=''){
              httpurl+=this.url.edit;
              method = 'put';
              if(this.select_type=='绣字字体'){
                this.select_type="0"
              }else if(this.select_type=='绣字图片'){
                this.select_type="1"
              }
              params = {embroideredName:this.category_name,
                code:this.category_code,
                numbers:this.single_price,
                remark:this.remark,
                embroideredImage:this.fileList,
                isZitiImg:"1",
                xiuziSize:this.xiuzi_size,
                id:this.model.id}
            }else{
              httpurl+=this.url.add;
              method = 'post';
              params = {embroideredName:this.category_name,
                code:this.category_code,
                numbers:this.single_price,
                remark:this.remark,
                embroideredImage:this.fileList,
                isZitiImg:"1",
                xiuziSize:this.xiuzi_size,
                categoryId:this.$route.query.category_id
              }
            }
            httpAction(httpurl,params,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
                that.confirmLoading = false;
                that.close();
              }else{
                that.$message.warning(res.message);
                that.confirmLoading = false;
              }
            }).finally(() => {
              // that.confirmLoading = false;
              // that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>

<style scoped>

</style>